/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.ui.field;

import java.util.ArrayList;
import java.util.List;

import com.daikit.daikit4gxt.client.ui.cell.DkAsSimpleStringCell;
import com.daikit.daikit4gxt.client.ui.cell.MyAsSimpleStringListCell;
import com.daikit.daikit4gxt.client.ui.edit.CreateButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.CreateButtonSelectEvent.CreateButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.edit.DeleteButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.DeleteButtonSelectEvent.DeleteButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.edit.EditButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.EditButtonSelectEvent.EditButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.forms.DkEditorGrid;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.SupplierDetailPropertyAccess;
import com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor.SupplierDetailEditCreatePopup;
import com.daikit.daikit4gxtsample.shared.bean.StockDetailData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.daikit.daikit4gxtsample.shared.bean.WarehouseData;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;


/**
 * An Editor for SupplierDetail
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class SupplierDetailEditorGrid extends DkEditorGrid<SupplierDetailData>
{

	//	public class CellColumnResizer<T> implements ColumnWidthChangeHandler
	//	{
	//
	//		private final Grid<SupplierDetailData> grid;
	//		private final ColumnConfig<SupplierDetailData, T> column;
	//		private final ResizeCell<T> cell;
	//
	//		public CellColumnResizer(final Grid<SupplierDetailData> grid, final ColumnConfig<SupplierDetailData, T> column,
	//				final ResizeCell<T> cell)
	//		{
	//			this.grid = grid;
	//			this.column = column;
	//			this.cell = cell;
	//			setHeight(560);
	//		}
	//
	//		@Override
	//		public void onColumnWidthChange(final ColumnWidthChangeEvent event)
	//		{
	//			if (column == event.getColumnConfig())
	//			{
	//				final int w = event.getColumnConfig().getWidth();
	//
	//
	//				final int col = grid.getColumnModel().indexOf(column);
	//
	//				cell.setWidth(w - 20);
	//
	//				final ListStore<SupplierDetailData> store = grid.getStore();
	//				final int rows = store.size();
	//
	//				for (int i = 0; i < rows; i++)
	//				{
	//					final SupplierDetailData p = grid.getStore().get(i);
	//
	//					// option 1 could be better for force fit where all columns are
	//					// resized
	//					// would need to run deferred using DelayedTask to ensure only run
	//					// once
	//					//					final DelayedTask delayedTask = new DelayedTask()
	//					//					{
	//					//						@Override
	//					//						public void onExecute()
	//					//						{
	//					//							grid.getStore().update(p);
	//					//						}
	//					//					};
	//					//					delayedTask.delay(1);
	//
	//					//					// option 2
	//					Element parent = grid.getView().getCell(i, col);
	//					if (parent != null)
	//					{
	//						parent = parent.getFirstChildElement();
	//						final SafeHtmlBuilder sb = new SafeHtmlBuilder();
	//						cell.render(new Context(i, col, store.getKeyProvider().getKey(p)), column.getValueProvider().getValue(p), sb);
	//						parent.setInnerHTML(sb.toSafeHtml().asString());
	//					}
	//
	//				}
	//			}
	//		}
	//	}


	/**
	 * Constructor
	 * 
	 * @param store
	 *           the store
	 */
	public SupplierDetailEditorGrid(final ListStore<SupplierDetailData> store)
	{
		super(store, getColumnConfigs(), SupplierDetailPropertyAccess.INSTANCE, Functionality.EDIT, Functionality.CREATE,
				Functionality.DELETE);
		setCreateLabel("Create SupplierDetail");
		setCreateTooltip("Create supplier detail tooltip");
		//		setHeadingHtml("TEST HEADING");

		addEditSelectHandler(new EditButtonSelectHandler<SupplierDetailData>()
		{
			@Override
			public void onSelect(final EditButtonSelectEvent<SupplierDetailData> event)
			{
				SupplierDetailEditCreatePopup.get().showForUpdate(getStore(), event.getEditedObject());
			}
		});

		addCreateSelectHandler(new CreateButtonSelectHandler()
		{
			@Override
			public void onSelect(final CreateButtonSelectEvent event)
			{
				SupplierDetailEditCreatePopup.get().showForCreation(getStore());
			}
		});

		addDeleteSelectHandler(new DeleteButtonSelectHandler<SupplierDetailData>()
		{
			@Override
			public void onSelect(final DeleteButtonSelectEvent<SupplierDetailData> event)
			{
				getStore().remove(event.getDeletedObject());
			}
		});


		//		final ProgressBarCell progress = new ProgressBarCell()
		//		{
		//			@Override
		//			public boolean handlesSelection()
		//			{
		//				return true;
		//			}
		//		};
		//		progress.setProgressText("{0}% Complete");
		//		progress.setWidth(140);
		//		final ColumnConfig columnProgress = getGrid().getColumnModel().getColumn(getGrid().getColumnModel().getColumnCount() - 3);
		//		columnProgress.setCell(progress);
		//		getGrid().getColumnModel().addColumnWidthChangeHandler(new CellColumnResizer<Double>(getGrid(), columnProgress, progress));
	}

	private static final List<ColumnConfig<SupplierDetailData, ?>> getColumnConfigs()
	{
		final List<ColumnConfig<SupplierDetailData, ?>> supplierDetailColumnConfigs = new ArrayList<ColumnConfig<SupplierDetailData, ?>>();
		final ColumnConfig<SupplierDetailData, String> columnSupplierDetailId = new ColumnConfig<SupplierDetailData, String>(
				SupplierDetailPropertyAccess.INSTANCE.id(), 150, Main.gwtSampleI18n().supplier_detail_id());
		final ColumnConfig<SupplierDetailData, String> columnSupplierDetailName = new ColumnConfig<SupplierDetailData, String>(
				SupplierDetailPropertyAccess.INSTANCE.name(), 150, Main.gwtSampleI18n().supplier_detail_name());
		final ColumnConfig<SupplierDetailData, SupplierData> columnSupplierDetailSupplier = new ColumnConfig<SupplierDetailData, SupplierData>(
				SupplierDetailPropertyAccess.INSTANCE.supplier(), 150, Main.gwtSampleI18n().supplier_detail_supplier());
		final ColumnConfig<SupplierDetailData, WarehouseData> columnSupplierDetailWarehouse = new ColumnConfig<SupplierDetailData, WarehouseData>(
				SupplierDetailPropertyAccess.INSTANCE.warehouse(), 150, Main.gwtSampleI18n().supplier_detail_warehouse());
		final ColumnConfig<SupplierDetailData, List<StockDetailData>> columnSuplierDetails = new ColumnConfig<SupplierDetailData, List<StockDetailData>>(
				SupplierDetailPropertyAccess.INSTANCE.stockDetails(), 340, Main.gwtSampleI18n().supplier_detail_stock_details());
		//		final ColumnConfig<SupplierDetailData, Double> columnProgress = new ColumnConfig<SupplierDetailData, Double>(
		//				SupplierDetailPropertyAccess.INSTANCE.progress(), 140, Main.gwtSampleI18n().supplier_detail_progress());

		supplierDetailColumnConfigs.add(columnSupplierDetailId);
		supplierDetailColumnConfigs.add(columnSupplierDetailName);
		supplierDetailColumnConfigs.add(columnSupplierDetailSupplier);
		supplierDetailColumnConfigs.add(columnSupplierDetailWarehouse);
		supplierDetailColumnConfigs.add(columnSuplierDetails);
		//		supplierDetailColumnConfigs.add(columnProgress);

		columnSupplierDetailSupplier.setCell(new DkAsSimpleStringCell<SupplierData>());
		columnSupplierDetailWarehouse.setCell(new DkAsSimpleStringCell<WarehouseData>());
		columnSuplierDetails.setCell(new MyAsSimpleStringListCell<StockDetailData>());
		return supplierDetailColumnConfigs;
	}

	@Override
	public String getDeleteTooltip()
	{
		return "Delete supplier detail tooltip";
	}

	@Override
	protected String getEditTooltip()
	{
		return "Edit supplier detail tooltip";
	}
}
